package unipampa.extensions.games.pampaPife;

//import java.lang.Boolean;
//import java.util.Arrays;
//import java.util.Collection;


//import com.smartfoxserver.v2.core.SFSEventType;
//import com.smartfoxserver.v2.entities.Room;
//import com.smartfoxserver.v2.entities.RoomSize;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;
import unipampa.extensions.games.pampaPife.Carta;

public class PampaPifeExtension extends SFSExtension{
	public PampaPifeBaralho baralho;
	public volatile boolean gameStarted=false;
	public Carta cartaTopoDescarte;
	public int jogadorDaVez = 1;
	
	public void init(){
		trace("PampaPife Extension para SFS2X iniciado.");
		baralho = new PampaPifeBaralho();
		cartaTopoDescarte = new Carta();
		
		addRequestHandler("pronto", TratarPronto.class);
		addRequestHandler("iniciar", TratarIniciar.class);
		addRequestHandler("cartas", TratarCartas.class);
		addRequestHandler("comprarDoMonte", TratarCompraDoMonte.class);
		addRequestHandler("comprarDoDescarte", TratarCompraDoDescarte.class);
		addRequestHandler("descartarCarta", TratarDescartarCarta.class);
		addRequestHandler("bater", TratarBater.class);
		addRequestHandler("estouVivo", TratarVivo.class);
		addRequestHandler("deixarJogo", TratarSaida.class);
		addRequestHandler("log",resposta.class);
		addEventHandler(SFSEventType.USER_DISCONNECT, TratarDesconecta.class);
	}
	
	public void destroy(){
		super.destroy();
		trace("PampaPife game destroyed!");
	}
	
	void iniciaJogo(){
		if (gameStarted)
			throw new IllegalStateException("Game is already started!");
		
		gameStarted = true;

		int nplayers = getParentRoom().getSize().getUserCount();
		jogadorDaVez = getParentRoom().getOwner().getPlayerId();
		
		ISFSObject respObj = new SFSObject();
		respObj.putInt("t", jogadorDaVez );
		respObj.putInt("nplayers",nplayers );
		User player;
		
		String id[] = {"p1n","p2n","p3n","p4n","p5n","p6n","p7n","p8n","p9n","p10n"};
		// Salvando nomes dos jogadores
		for (int i=0;i<nplayers;i++){
			player = getParentRoom().getUserByPlayerId(i+1);
			respObj.putUtfString(id[i], ( player==null? "" : player.getName()));
		}
		trace("Enviando comando de inicio do jogo.");
		// Enviando comando de inicio do jogo
		send("start", respObj, getParentRoom().getUserList());
	}

	public void atualizaNumeroJogadores(){
		int nplayers = getParentRoom().getSize().getUserCount();
		ISFSObject respObj = new SFSObject();
		
		respObj.putInt("nplayers", nplayers);
		trace("Enviando atualizaNumJog para os jogadores.");
		send("atualizaNJ", respObj, getParentRoom().getUserList());
	}
	public void colocaNoDescarte(Carta c) {
		baralho.paraOMonte(c);
		cartaTopoDescarte = c;
	}
	public int proximoJogador(){
		int nplayers = getParentRoom().getSize().getUserCount();
		User player;
		boolean encontrou = false;
		while (!encontrou){
			jogadorDaVez++;
			if ( jogadorDaVez > nplayers )
				jogadorDaVez = 1;
			player = getParentRoom().getUserByPlayerId(jogadorDaVez);
			if (player != null)
				encontrou = true;
		}
		return jogadorDaVez;
	}
	public Carta cartaTopoDescarteFunc() {
		return cartaTopoDescarte;
	}
}